package com.wsd.exception;


import cn.hutool.http.HttpStatus;
import cn.hutool.http.HttpUtil;
import com.wsd.common.entity.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 全局异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(RRException.class)
    public ResponseEntity handleRRException(RRException e) {
        return ResponseEntity.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public ResponseEntity handleNotFoundException(Exception e) {
        log.error(e.getMessage(), e);
        return ResponseEntity.error(HttpStatus.HTTP_NOT_FOUND, "访问的路径不存在");
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public ResponseEntity handleDuplicateKeyException(DuplicateKeyException e) {
        log.error(e.getMessage(), e);
        return ResponseEntity.error("数据库中已存在该记录");
    }

    @ExceptionHandler(NoPermissionException.class)
    public ResponseEntity handleNoPermissionException(NoPermissionException e) {
        log.error(e.getMessage(), e);
        return ResponseEntity.error("没有权限访问, 请进行授权登录后再操作");
    }

    @ExceptionHandler(AuthenticationException.class)
    public ResponseEntity handleAuthenticationException(AuthenticationException e) {
        log.error(e.getMessage(), e);
        return ResponseEntity.error("没有权限");
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity handleException(Exception e){
        log.error(e.getMessage(), e);
        return ResponseEntity.error(e.getMessage());
    }

}
